import ComResUrl from "src/_T/Res/ComResUrl";


/**
 * 音效工具类
 * 必须要先预加载音效，不然播放会有延迟
 */
export default class AudioUtils {
    //
    private static _instance: AudioUtils;
    /** 单例 */
    public static get instance(): AudioUtils {
        if (this._instance == null) {
            this._instance = new AudioUtils();
        }
        return this._instance;
    }
    //
    private constructor() { }

    //初始化
    public init() { }

    //
    private _bgPast: string[] = [];
    private _urlBGM: string = '';
    private _urlSOUND: string = '';

    /**
     * 播放背景音乐
     * @param name 背景音乐名字
     */
    public playBGM(name: string, loops?: number, complete?: Handler, startTime?: number): void {
        if (name != null && this._bgPast.slice(-1)[0] != name) {
            this._bgPast.push(name);
            this._urlBGM = ComResUrl.MusicURL(name);
            this._playMusic(loops, complete, startTime);
            //console.log(...ConsoleEx.packLog("播放背景音乐", name));
        }
        else {
            if (this._urlBGM != "") {
                this._playMusic(loops, complete, startTime);
                //console.log(...ConsoleEx.packLog("播放背景音乐", name));
            } else {
            }
        }
    }

    /**
     * 转移背景音乐
     * @param name 
     */
    public shiftBGM(name: string, loops?: number, complete?: Handler, startTime?: number): void {
        if (this._bgPast.slice(-1)[0] == name) {
            this._bgPast.pop();
            let pastBg: string = this._bgPast.slice(-1)[0];
            if (pastBg) {
                this._urlBGM = ComResUrl.MusicURL(name);
                this._playMusic(loops, complete, startTime);
            }
        }
    }

    /**
     * 暂停背景音乐
     */
    public pauseBGM() {
        Laya.SoundManager.stopMusic();
        //console.log(...ConsoleEx.packLog("停止播放音乐", this._urlBGM));
    }

    /**
     * 暂停特效音乐
     */
    public pauseSound() {
        Laya.SoundManager.stopAllSound();
    }

    private _sounds: Laya.SoundChannel[] = [];
    /**
     * 播放特效音乐
     * @param type 名字
     * @param loops 是否循环
     * @param complete 完成回调
     * @param soundClass 使用哪个声音类进行播放，null表示自动选择。
     * @param startTime 开始时间
     */
    public playSound(type: string, loops?: number, complete?: Laya.Handler, soundClass?: any, startTime?: number): void {

        console.log("播放音效");
        this._urlSOUND = ComResUrl.SoundURL(type);
        if (this._sounds.length <= 5) {
            let temp = null;
            if (complete == null) {
                let Selfcomplete = Laya.Handler.create(this, (Sounds) => {
                    for (let i = 0; i < this._sounds.length; i++) {
                        if (this._sounds[i] == temp) {
                            //if (this._sounds[i].url.indexOf(this._urlSOUND) >= 0) {
                            console.log("停止音效");
                            //this._sounds[i].stop();
                            Laya.SoundManager.removeChannel(this._sounds[i]);
                            this._sounds.splice(i, 1);
                            break;
                            //}
                        } else if (this._sounds[i] == null) {
                            this._sounds.splice(i, 1);
                        } else if (this._sounds[i].isStopped) {
                            Laya.SoundManager.removeChannel(this._sounds[i]);
                            this._sounds.splice(i, 1);
                        }
                    }
                });
                temp = Laya.SoundManager.playSound(this._urlSOUND, loops, Selfcomplete, soundClass, startTime);
            } else {
                temp = Laya.SoundManager.playSound(this._urlSOUND, loops, complete, soundClass, startTime);
            }

            if (temp != null) {
                temp.volume = 1;
                this._sounds.push(temp);
            }
            console.log(temp);
        } else {
            for (let i = 0; i < this._sounds.length; i++) {
                if (this._sounds[i] == null) {
                    this._sounds.splice(i, 1);
                } else if (this._sounds[i].isStopped) {
                    Laya.SoundManager.removeChannel(this._sounds[i]);
                    this._sounds.splice(i, 1);
                }
            }
        }
    }

    DeleteSound(type: string) {
        this._urlSOUND = ComResUrl.SoundURL(type);
        for (let i = 0; i < this._sounds.length; i++) {
            if (this._sounds[i].url.indexOf(this._urlSOUND) >= 0) {
                // {
                console.log("停止音效");
                //this._sounds[i].stop();
                Laya.SoundManager.removeChannel(this._sounds[i]);
                this._sounds.splice(i, 1);
                break;
                //}
            } else if (this._sounds[i] == null) {
                this._sounds.splice(i, 1);
            } else if (this._sounds[i].isStopped) {
                Laya.SoundManager.removeChannel(this._sounds[i]);
                this._sounds.splice(i, 1);
            }
        }
    }


    /**
     * 停止播放某个特效音乐
     * @param type 
     */
    public stopSound(type: string): void {
        this._urlSOUND = ComResUrl.SoundURL(type);
        Laya.SoundManager.stopSound(this._urlSOUND);
    }

    // 播放音乐后的回调
    private _playMusic(loops: number = 0, complete?: Handler, startTime?: number) {
        Laya.SoundManager.stopMusic();
        Laya.SoundManager.playMusic(this._urlBGM, loops, complete, startTime);
    }
}